package com.intelligencesoft.pss.filter;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.intelligencesoft.pss.auth.LoginType;
import com.intelligencesoft.pss.auth.LoginUtil;
import com.intelligencesoft.pss.entity.base.UserInfo;
import com.intelligencesoft.pss.utils.AuthUtil;
import com.intelligencesoft.pss.utils.PropertiesUtil;
import com.intelligencesoft.pss.utils.auth.Base64Util;

@WebFilter(filterName = "authFilter", urlPatterns = { "/*" })
@Component("pss_auth_filter")
public class AuthFilter implements Filter{
	private FilterConfig config;
	private Cookie cookie = new SimpleCookie();
	
	public AuthFilter() {}
	/**
	 * 忽略的地址, 正则表达式
	 */
	private String[] excludes;
	private String pssAuth;
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) res;
		boolean isLogin=false;
		
		if (AuthUtil.isExclude(request,excludes)) {
			isLogin=true;
		}
//		= new LoginUtil();
		if (getLoginUser(request) != null) {
			isLogin=true;
		}
		if (isLogin) {
			chain.doFilter(request, response);
		}else{
			String requestURL = request.getRequestURL().toString();
			response.sendRedirect(AuthUtil.stringToBuffer(pssAuth,"/views/login.jsp?goto="+requestURL));
		}
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
		ServletContext context = config.getServletContext();
		Properties ps = PropertiesUtil.getProperties(context);
		excludes = PropertiesUtil.getExcludesUrl(ps);
		pssAuth = PropertiesUtil.getAuthUrl(ps);
		
	}
	
	private String getLoginInfo(HttpServletRequest request) {
		cookie.setName(LoginType.LOGINCOOKIE);
		String info = cookie.readValue(request, null);
		if(info==null){
			info = request.getParameter("tokenId");
		}
		return Base64Util.decode(info);
	}
	
	public UserInfo getLoginUser(HttpServletRequest request) {
		String info = getLoginInfo(request);
		if (null != info && !"".equals(info)) {
			try {
				return new ObjectMapper().readValue(info, UserInfo.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
